$(function () {
    /*$("#jqGrid").jqGrid({
        url: baseURL + 'dev/config/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
			{ label: '商户编号', name: 'uid', index: 'uid', width: 80 }, 			
			{ label: '标题', name: 'name', index: 'name', width: 80 }, 			
			{ label: '', name: 'appId', index: 'app_id', width: 80 }, 			
			{ label: '', name: 'appSecret', index: 'app_secret', width: 80 }, 			
			{ label: '', name: 'mchId', index: 'mch_id', width: 80 }, 			
			{ label: '', name: 'apiKey', index: 'api_key', width: 80 }, 			
			{ label: '', name: 'accessKeyId', index: 'access_key_id', width: 80 }, 			
			{ label: '', name: 'accessKeySecret', index: 'access_key_secret', width: 80 }, 			
			{ label: '注册时间', name: 'createTime', index: 'create_time', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });*/
	
	new AjaxUpload('#upload', {
        action: baseURL + "dev/config/upload",
        name: 'picfile',
        autoSubmit:true,
        responseType:"json",
        onSubmit:function(file, extension){
          if (!(extension && /^(p12)$/.test(extension.toLowerCase()))){
                alert('只支持.p12证书文件！');
                return false;
            }
        },
        onComplete : function(file, r){
            if(r.code == 0){
            	console.log(r.url);
            	vm.config.certName = r.certName;
            	vm.config.certFile = r.certFile;
            	console.log(vm.config);
            }else{
                alert(r.msg);
            }
        }
    });
	
	new AjaxUpload('#uploadbg', {
        action: baseURL + "dev/config/uploadbg",
        name: 'picfile',
        autoSubmit:true,
        responseType:"json",
        onSubmit:function(file, extension){
        	if (!(extension && /^(jpg|jpeg|png)$/.test(extension.toLowerCase()))){
                alert('只支持jpg、jpeg、png格式的图片！');
                return false;
            }
        },
        onComplete : function(file, r){
            if(r.code == 0){
            	console.log(r.url);
            	vm.config.bg = r.url;
            	console.log("111"+vm.config.bg);
            }else{
                alert(r.msg);
            }
        }
    });
	
	new AjaxUpload('#uploadbg2', {
        action: baseURL + "dev/config/uploadrecommend",
        name: 'picfile',
        autoSubmit:true,
        responseType:"json",
        onSubmit:function(file, extension){
        	if (!(extension && /^(jpg|jpeg|png)$/.test(extension.toLowerCase()))){
                alert('只支持jpg、jpeg、png格式的图片！');
                return false;
            }
        },
        onComplete : function(file, r){
            if(r.code == 0){
            	console.log(r.url);
            	vm.config.recommendPic = r.url;
            	console.log("222"+vm.config.recommendPic);
            }else{
                alert(r.msg);
            }
        }
    });
	
	
	
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: false,
		title: "全局配置",
		type:1,
		config: {bg:'bg.jpg'}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.config = {bg:'bg.jpg',recommendCode:0,recommendMoney:0};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.config.id == null ? "dev/config/save" : "dev/config/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.config),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "dev/config/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(){
			console.log('查询配置信息 ')
			$.get(baseURL + "dev/config/info", function(r){
                vm.config = r.config;
                console.log(vm.config);
            });
		},
		reload: function (event) {
			vm.getInfo();
			/*vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");*/
		}
	},
	created: function () {
		// `this` 指向 vm 实例
		//this.getInfo();
		console.log('created 实例被创建之后被调用 ')
	},
	mounted(){
		
	    console.log("页面加载完成")
	    this.getInfo();
	}
});

